<?php

namespace app\controller;

use app\BaseController;
use app\model\SysRegion;
use boc\Boc;
use boc\YeePay;
use sheng_pay\SPay;
use Symfony\Component\VarDumper\Caster\Caster;
use think\facade\Cache;
use think\helper\Str;
use think\Response;
use think\response\Json;
use yeepay\YopRequest;
use yeepay\YopRsaClient;
use yly_print\YlyOpen;

class Index extends BaseController
{
    public function index()
    {
        return redirect("/ads");
//        return '<style>*{ padding: 0; margin: 0; }</style><iframe src="https://www.thinkphp.cn/welcome?version=' . \think\facade\App::version() . '" width="100%" height="100%" frameborder="0" scrolling="auto"></iframe>';
    }

    public function hello($name = 'ThinkPHP8')
    {
//        dd(round(1.135,2,PHP_ROUND_HALF_EVEN), round(1.135,2,PHP_ROUND_HALF_DOWN), round(1.135,2,PHP_ROUND_HALF_UP),
//        round(1.356,2,PHP_ROUND_HALF_EVEN),
//            round(1.356,2,PHP_ROUND_HALF_DOWN), round(1.356,2,PHP_ROUND_HALF_UP),);
//        return 'hello,' . $name;
        $dat = push_notice("https://zhissf.verjs.cn/index/t", ["title" => "测试", "content" => "测试内容"]);
        dd($dat);
//        dd(md5('9627270117554986719sVchbzuePexswxFmymeXJkUj3n5kDbj'));
//        dd(
//            round(1.136, 2, PHP_ROUND_HALF_EVEN),
//            round(1.136, 2, PHP_ROUND_HALF_DOWN),#五舍六入
//            round(1.136, 2),
//            ceil(1.1356 * 1000),
//            round(ceil(1.1356 * 1000) / 1000, 2, PHP_ROUND_HALF_DOWN)
//        );
    }

    public function t()
    {
        $param = $this->request->param();
        return json($param);
    }

    public function areaAsync()
    {
//        Cache::delete("amap_city");
        if (!Cache::has("amap_city")) {
            $data = file_get_contents('https://restapi.amap.com/v3/config/district?key=f6ddac558ede9acc07265df6b72da88d&keywords=中国&subdistrict=3&extensions=base');
//        dd($data);
            $data = json_decode($data, true);
            if (empty($data) || $data['status'] != 1) throw new \Exception('获取数据失败');
//            Cache::set("amap_city", $data['districts'], 7 * 24 * 60 * 60);
            $data = $data['districts'][0]['districts'];
            //按照adcode 进行排序
            usort($data, function ($a, $b) {
                return $a['adcode'] - $b['adcode'];
            });
            Cache::set("amap_city", $data, 7 * 24 * 60 * 60);
        } else {
            $data = Cache::get("amap_city");
        }
        $items = $this->filterCity($data);
        @file_put_contents("./city.json", json_encode(['row' => $items], 320));
        return json($items);
    }

    private function filterCity($city, $parent = ""): array
    {
        $items = [];
        if (!empty($city)) {
            foreach ($city as $its) {
                $dis = $its['districts'];
                usort($dis, function ($a, $b) {
                    return $a['adcode'] - $b['adcode'];
                });
                unset($its['districts']);
                $its['parent'] = $parent;
                $items[] = $its;
                if (!empty($dis)) {
                    $items = array_merge($items, $this->filterCity($dis, $its['adcode']));
                }
            }
        }
        return $items;
    }

    public function cityTree(): Response
    {
        return $this->success($this->app->make(SysRegion::class)->cityTree());
    }

    public function refund(): Json
    {
        $order = $this->request->param("order");
        $money = $this->request->param("money");
        $remark = $this->request->param("remark");
        return json($this->app->make(SPay::class)->refund($money, $order, "R$order", $remark, "https://aaa"));
    }

    public function ylyOauth()
    {
        $data = (new YlyOpen())->getTokenBySecret($this->request->param("code"), $this->request->param('token'));
        return json($data);
    }

    public function test()
    {
//        $request = new YopRequest("app_10091763741", "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC4S4h08MV5/NCL/T3fedB4EuzJvlcev6obXHocPTJlRFKxRwtX5G2Ne8xnvShoTBjlwBSlEpxdBq0E4VwS9gUIZq0NEmA5KaQSPg+guKML+QLe16B9kq+lpuLTiES+ysuRbfLBS/QSgRzUfa44YOl5S4TayLG07czIpTLHWajJyAFi7DS5N/Vvg0nujHPkyFO+9cInuc/TX61XWGVg9lOf/4V638hjpZ6uJ24EDRPgBZGHUDbkTrXcDoNqYWyl9DCBG+VKP3RjS20U2Vbptz/nvUozpxRwM/J+UF0+4CBoPxtRDL9ddFRZvNjYnHftUj88gsKYT1n5ybaUpVN9veYbAgMBAAECggEAUVaA3L/RIFRMxKQZtTKBqCUOHcIkmD0WOio3+JO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mu/jF6bDZdMsAH+76oAzJtdvmT2UE7pdkeBAUjIcSSkoTLJykme13OULmn7r9VVwhKOg3MWW1j9NVtLcS3UR9fmypw0kmm+N4=");
//        $request->addParam("parentMerchantNo", "10091763741");
//        $request->addParam("merchantNo", "10091763741");
//        $request->addParam("payerMerchantNo", "10091763504");
//        $request->addParam("orderId", date("YmdHis") . Str::random(6, 1));
//        $request->addParam("orderAmount", "0.01");
//        $request->addParam("goodsName", 'Api代扣');
//        $request->addParam("payerNotifyUrl", '/YeePayNotify/pushPay');
//        $request->addParam("fundProcessType", "DELAY_SETTLE");
//        $data = YopRsaClient::post("/rest/v1.0/account/enterprise/withholding/order", $request);
//        dd(json_decode(json_encode($data, 320), true));
        return \json($this->app->make(YeePay::class)->selfSettle( "10091769880"));
    }
}
